package com.summer.filesync.model.manager;

import com.summer.filesync.model.FileMetadata;
import java.util.List;

/** 重复文件组 表示一组具有相同内容的文件 */
public class DuplicateFileGroup {
  private String checksum; // 修改：使用checksum替代fileHash
  private List<FileMetadata> files;
  private long fileSize;
  private long wastedSpace;
  private int duplicateCount;

  public DuplicateFileGroup(String checksum, List<FileMetadata> files) {
    this.checksum = checksum;
    this.files = files;
    this.duplicateCount = files.size();

    if (!files.isEmpty()) {
      this.fileSize = files.get(0).getFileSize();
      this.wastedSpace = (duplicateCount - 1) * fileSize;
    }
  }

  /** 获取建议保留的文件（通常是最新的） */
  public FileMetadata getRecommendedKeepFile() {
    return files.stream()
        .max((f1, f2) -> Long.compare(f1.getLastModified(), f2.getLastModified()))
        .orElse(null);
  }

  /** 获取建议删除的文件列表 */
  public List<FileMetadata> getRecommendedDeleteFiles() {
    FileMetadata keepFile = getRecommendedKeepFile();
    return files.stream().filter(f -> !f.equals(keepFile)).toList();
  }

  // Getters and Setters
  public String getChecksum() {
    return checksum;
  }

  public void setChecksum(String checksum) {
    this.checksum = checksum;
  }

  public List<FileMetadata> getFiles() {
    return files;
  }

  public void setFiles(List<FileMetadata> files) {
    this.files = files;
  }

  public long getFileSize() {
    return fileSize;
  }

  public void setFileSize(long fileSize) {
    this.fileSize = fileSize;
  }

  public long getWastedSpace() {
    return wastedSpace;
  }

  public void setWastedSpace(long wastedSpace) {
    this.wastedSpace = wastedSpace;
  }

  public int getDuplicateCount() {
    return duplicateCount;
  }

  public void setDuplicateCount(int duplicateCount) {
    this.duplicateCount = duplicateCount;
  }
}
